#include "llm_infer_native.h"
#include "llm_infer.h" // 包含原始的LLMInfer类定义
#include <string>      // 用于 strcpy 等操作

// 创建LLMInfer对象并返回其句柄
LLMInferHandle_t LLMInfer_Native_Create() {
    return new LLMInfer(); // 返回新创建的C++对象指针
}

// 销毁LLMInfer对象
void LLMInfer_Native_Destroy(LLMInferHandle_t handle) {
    if (handle) {
        auto instance = static_cast<LLMInfer *>(handle);
        instance->Destroy();
        delete static_cast<LLMInfer *>(handle); // 将void*转换回LLMInfer*并删除
    }
}

bool LLMInfer_Native_Generate_Is_End(LLMInferHandle_t handle) {
    if (!handle)
        return true;
    LLMInfer *instance = static_cast<LLMInfer *>(handle);
    return instance->IsEnd();
}

int LLMInfer_Native_Stop_Generate(LLMInferHandle_t handle) {
    if (!handle)
        return -1;
    LLMInfer *instance = static_cast<LLMInfer *>(handle);
    instance->Stop();
    return 0;
}

// 封装Build方法
int LLMInfer_Native_Build(LLMInferHandle_t handle, const char *model_file_path, const char *generate_params,
                          bool is_record_perf_data) {
    if (!handle)
        return -1; // 错误处理：无效句柄
    LLMInfer *instance = static_cast<LLMInfer *>(handle);
    int ret = instance->Build(generate_params, model_file_path, is_record_perf_data);
    if (ret != 0) {
        LLMInfer_Native_Destroy(handle);
    }
    return ret;
}

// 封装Generate方法
int LLMInfer_Native_Generate(LLMInferHandle_t handle, const char *input_text, char **output_text) {
    if (!handle || !input_text || !output_text)
        return -1;

    LLMInfer *instance = static_cast<LLMInfer *>(handle);
    std::string gene_text; // 接收C++方法输出的string

    int result = instance->Generate(input_text, gene_text);

    if (result == 0) { // 假设0表示成功
        // 为C字符串分配内存并复制内容
        *output_text = new char[gene_text.size() + 1];
        std::strcpy(*output_text, gene_text.c_str());
    } else {
        *output_text = nullptr;
    }
    return result;
}

int LLMInfer_Native_Get_Generate_Async_Result(LLMInferHandle_t handle, char **output_text) {

    if (!handle || !output_text)
        return -1;

    LLMInfer *instance = static_cast<LLMInfer *>(handle);
    std::string gene_text; // 接收C++方法输出的string

    int result = instance->GetAsyncResult(gene_text);

    if (result == 0) { // 假设0表示成功
        // 为C字符串分配内存并复制内容
        *output_text = new char[gene_text.size() + 1];
        std::strcpy(*output_text, gene_text.c_str());
    } else {
        *output_text = nullptr;
    }
    return result;
}

// 封装Generate方法
int LLMInfer_Native_Generate_Async(LLMInferHandle_t handle, const char *input_text) {
    if (!handle || !input_text)
        return -1;

    LLMInfer *instance = static_cast<LLMInfer *>(handle);
    return instance->GenerateAsync(input_text);
}

// 封装Generate with callback方法
int LLMInfer_Native_Generate_With_Perf_CallBack(LLMInferHandle_t handle, const char *input_text, char **output_text,
                                                Generate_Callback_t callback, void *user_data) {
    if (!handle || !input_text || !output_text)
        return -1;

    LLMInfer *instance = static_cast<LLMInfer *>(handle);
    std::string gene_text; // 接收C++方法输出的string
    int result = instance->GenerateWithPerfStat(input_text, gene_text);

    if (result == 0) { // 假设0表示成功
        // 为C字符串分配内存并复制内容
        *output_text = new char[gene_text.size() + 1];
        std::strcpy(*output_text, gene_text.c_str());
    } else {
        *output_text = nullptr;
    }
    const char *pref_data = instance->GetPerfStatString();
    if (callback != NULL) {
        callback(pref_data, user_data);
    }

    return result;
}

int LLMInfer_Native_Generate_Stream(LLMInferHandle_t handle, const char *input_text) {
    if (!handle || !input_text)
        return -1;

    LLMInfer *instance = static_cast<LLMInfer *>(handle);

    instance->GenerateStream(input_text);
    return 0;
}

int LLMInfer_Native_Stream_Word(LLMInferHandle_t handle, char **output_text, bool *is_end) {
    if (!handle || !output_text)
        return -1;

    LLMInfer *instance = static_cast<LLMInfer *>(handle);
    Word *word = instance->GetStreamOneWord();
    if (word != nullptr) { // 假设0表示成功
        // 为C字符串分配内存并复制内容
        *output_text = new char[word->words.size() + 1];
        std::strcpy(*output_text, word->words.c_str());
        *is_end = word->is_end;
        delete word;
    } else {
        *output_text = nullptr;
    }
    return 0;
}